<?php

class Zy_addcase extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		header("Content-type:text/html;charset=utf-8");
		
		$this->load->helper('form');
		$this->load->helper('url');
	}

	function index()
	{
		/*if(strpos($_SERVER["HTTP_USER_AGENT"], "Chrome"))
		{

		}
		else*/
		{
			$this->load->view('Zy_view_addcase');
		}
	}

	function clear_files($files)
	{
		$uploads_dir = $this->load->get_var('uploaddir')."/";

		for ($i = 0; $i < $files[$i]; $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}
			
			unlink(iconv("utf-8","gb2312",$uploads_dir.$files[$i]));
		}
	}

	function rename_files($name, $date, &$files)
	{
		$uploads_dir = $this->load->get_var('uploaddir')."/";

		for ($i = 0; $i < count($files); $i++)
		{
			if (!strcmp($files[$i], ""))
			{
				continue;
			}

			$newfile = $name."_".$date."_".$files[$i];

			if (rename($uploads_dir.$files[$i], iconv("utf-8","gb2312",$uploads_dir.$newfile)))
			{
				$files[$i] = $newfile;
			}
		}
	}
	
	function upload(&$files)
	{
		$uploads_dir = $this->load->get_var('uploaddir')."/";

		if (!file_exists ($uploads_dir))
		{
			if (!mkdir($uploads_dir))
			{
				return -1;
			}
		}

		$config['upload_path'] = $uploads_dir;
		$config['allowed_types'] = 'gif|jpg|png|bmp|pdf|rar|zip';
		$config['max_size'] = '10240';
		$config['remove_spaces'] = TRUE;
		
		$config['file_name'] = time();
		$this->load->library('upload', $config);

		for ($i = 0; $i < 5; $i++)
		{
			$files[$i] = "";

			$fieldname = "patient_file".($i + 1);
			$fieldvalue = $_FILES[$fieldname]['name'];

			if (empty($fieldvalue))
			{
				continue;
			}

			if(!$this->upload->do_upload($fieldname))
			{
				echo $this->upload->display_errors();
				$this->clear_files($files);
				return -2;
			}
	
			$updata = $this->upload->data();
			$img = $updata['file_name'];

			$files[$i] = $img;
		}

		return 0;
	}

	function newprescribe($prescribe_name, $prescribe_value, $prescribe_usemethod)
	{
		if (!$prescribe_name || !$prescribe_value)
		{
			return;	
		}

		$prescribetable = $this->load->get_var('prescribetable');

		$prescribe_name = Myphplib::trim_space($prescribe_name);

		if (!strcmp($prescribe_name, ""))
		{
			return;	
		}

		if (!strcmp($prescribe_value, ""))
		{
			return;	
		}

		$prescribe_namestr = $this->db->escape_str($prescribe_name);
		
		$querystring = "select * from $prescribetable where `name`='$prescribe_namestr'";
		$query = $this->db->query($querystring);
		
		if (!$query)
		{
			return;
		}

		if ($query->num_rows() != 0)
		{
			return;
		}

		$data = array(
			'name' => $prescribe_name,
			'value' => $prescribe_value,
			'usemethod' => $prescribe_usemethod,
			);

		$querystring = $this->db->insert_string($prescribetable, $data);
		
		if (!$this->db->query($querystring))
		{
			return;
		}
	}
	
	function add()
	{
			$dbtable = $this->load->get_var('dbtable');

			$patient_name=$this->input->post('patient_name');
			$patient_name = Myphplib::trim_space($patient_name);
			
			if (!strcmp($patient_name, ""))
			{
				echo "请输入姓名!";
				return;	
			}
			
			$patient_sex=$this->input->post('patient_sex');
			
			if (!strcmp($patient_sex, ""))
			{
				echo "请输入性别!";
				return;	
			}

			$patient_age=$this->input->post('patient_age');	
			$ageunit=$this->input->post('ageunit');
			
			if (!strcmp($ageunit, 'M'))
			{
				$patient_age -= 1000;
			} 

			$patient_date=$this->input->post('patient_date');	
			
			if (!strcmp($patient_date, ""))
			{
				echo "请输入日期!";
				return;	
			}
			
			$patient_attackdate=$this->input->post('patient_attackdate');	

			$patient_phonenum=$this->input->post('patient_phonenum');	
			$patient_phonenum = Myphplib::trim_space($patient_phonenum);
			
			$patient_addr=$this->input->post('patient_addr');	
			
			$patient_diag=$this->input->post('patient_diag');
			
			if (!strcmp($patient_diag, ""))
			{
				echo "请输入病史!";
				return;	
			}
			
			$patient_prescribe=$this->input->post('patient_prescribe');
			$patient_usemethod=$this->input->post('patient_usemethod');
			$patient_comment=$this->input->post('patient_comment');

			$this->load->database();

			$files = array();

			if ($this->upload($files) < 0)
			{
				echo "上传文件失败!";
				return;
			}

			$this->rename_files($patient_name, $patient_date, $files);

			$files_str = "";

			for ($i = 0; $i < count($files); $i++)
			{
				if (!strcmp($files[$i], ""))
				{
					continue;
				}

				$files_str .= $files[$i].";";
			}

			$data = array(
				'name' => $patient_name, 'sex' => $patient_sex, 'age' => $patient_age, 'date' => $patient_date,
				'diag' => $patient_diag,
				'prescribe' => $patient_prescribe,
				'usemethod' => $patient_usemethod,
				'comment' => $patient_comment,
				'phonenum' => $patient_phonenum,
				'addr' => $patient_addr,
				'files' => $files_str,
				'attackdate' => $patient_attackdate,
				);

			$querystring = $this->db->insert_string($dbtable, $data);
			
			if (!$this->db->query($querystring))
			{
				echo "添加病例失败!";
				$this->clear_files($files);
				return;
			}

			$prescribename=$this->input->post('prescribename');	
			$patient_newprescribe=$this->input->post('patient_newprescribe');	
			
			if ($patient_newprescribe && !strcmp($patient_newprescribe, "on"))
			{
				$this->newprescribe($prescribename, $patient_prescribe, $patient_usemethod);
			}
			
			$this->load->view('Zy_view_addcasesuccess');
	}
}

?>